﻿using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SharedClientDll.FormShared
{
    public partial class FormPublic<T> : Form
        where T : TableBase, new()
    {
        private T _tw = null;
        private List<string> _showTitle = null;
        //private Control _con = null;
        private T _choice = null;
        private List<T> _listT = null;

        public FormPublic(List<string> title)
        {
            _showTitle = title;
            InitializeComponent();
        }

        public FormPublic(T tWhere,List<string> title)
        {
            _tw = tWhere;
            _showTitle = title;
            InitializeComponent();
        }


        public FormPublic(List<string> title,List<T> ts)
        {
            //_tw = tWhere;
            _showTitle = title;
            _listT = ts;
            InitializeComponent();
        }

        private void AddListViewData(T t)
        {
            if (_showTitle == null || _showTitle.Count == 0) { Close(); return; }

            try
            {
                var item = new ListViewItem();
                for (int i = 0; i < _showTitle.Count; i++)
                {
                    var property = typeof(T).GetProperty(_showTitle[i]);

                    if (property == null) continue;

                    if (i == 0) item.Text = property.GetValue(t).ToString();
                    else if (property.GetValue(t) == default(T)) item.SubItems.Add(string.Empty);
                    else item.SubItems.Add(property.GetValue(t).ToString());
                }

                this.listViewPublic.Items.Add(item);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Close();
                //return;
            }
        }

        private void FormPublic_Load(object sender, EventArgs e)
        {
            //展现数据内容
            SetListViewTitle();

            if (_listT == null || _listT.Count == 0)
            {

                if (_tw == null) _tw = new T();
                var selectValue = Client.ClientMethod.Select(_tw);
                if(selectValue.Result&& selectValue.Value.Count >= 1)
                {
                    _listT = selectValue.Value;
                }
            }

            if (_listT!=null&&_listT.Count>=1)
            {
                //添加数据内容到表格中
                for(int i = 0; i < _listT.Count; i++)
                {
                    AddListViewData(_listT[i]);
                }
            }
            else
            {
                MessageBox.Show("未能查到相应数据，即将关闭");
                Close();
            }
        }


        private void ListViewPublic_MouseClick(object sender, MouseEventArgs e)
        {
            string choiceText = this.listViewPublic.SelectedItems[0].Text;

            var property = typeof(T).GetProperty(_showTitle[0]);

            _choice = (from t in _listT
                       where property.GetValue(t).ToString() == choiceText
                       select t).FirstOrDefault();

            Close();
            return;
        }

        public T GetChoice()
        {
            return _choice;
        }

        //添加LISTVIEW表头

        private void SetListViewTitle()
        {
            try
            {
                if (_showTitle == null || _showTitle.Count == 0)
                {
                    MessageBox.Show("未传入表头内容");
                    Close();
                    return;
                }

                //获取T对应的所有表头
                this.listViewPublic.Columns.Clear();//删除表头

                for (int i = 0; i < _showTitle.Count; i++)
                {
                    var property = typeof(T).GetProperty(_showTitle[i]);

                    if (property == null) continue;

                    var header = new ColumnHeader
                    {
                        Text = property.Name,
                        Width = 120
                    };

                    this.listViewPublic.Columns.Add(header);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                Close();
                return;
            }
        }

        private void ClearListView()
        {
            this.listViewPublic.Items.Clear();
        }

        private void TextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            string searchText = this.textBoxSearch.Text.Trim();

            if (string.IsNullOrEmpty(searchText))
            {
                ClearListView();
                for (int i = 0; i < _listT.Count; i++)
                {
                    AddListViewData(_listT[i]);
                }
                return;
            }

            //使用LIST搜索
            var searchList = new List<T>();

            string tStr = string.Empty;
            for(int i = 0; i < _listT.Count; i++)
            {
                //获得T的字符串表达式
                tStr = string.Empty;
                for(int j = 0; j < _showTitle.Count; j++)
                {
                    var property = typeof(T).GetProperty(_showTitle[j]);
                    tStr += property.GetValue(_listT[i]).ToString()+"_";
                }

                if (tStr.Contains(searchText)) searchList.Add(_listT[i]);
            }

            if (searchList != null && searchList.Count >= 1)
            {
                ClearListView();

                for(int i = 0; i < searchList.Count; i++)
                {
                    AddListViewData(searchList[i]);
                }
            }
        }
    }
}
